package nyist.media.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import nyist.media.entity.ApplyRecord;
import nyist.media.entity.ClassRoom;
import nyist.media.entity.RepairRecord;
import nyist.media.entity.admin.Admin;
import nyist.media.entity.admin.Role;
import nyist.media.enums.ApplyStatus;
import nyist.media.enums.RepairStatus;
import nyist.media.enums.RoomType;
import nyist.media.exception.BusinessException;
import nyist.media.page.ApiResult;
import nyist.media.page.AppPage;
import nyist.media.repository.ApplyRecordRepository;
import nyist.media.repository.ClassRoomRepository;
import nyist.media.repository.RoleRepository;
import nyist.media.service.ClassRoomService;
import nyist.media.vo.ClassRoomVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author : dcf
 * @date : 2019/3/15
 */

@Service
public class ClassRoomServiceImpl implements ClassRoomService {

    @Autowired
    private ClassRoomRepository classRoomRepository;
    @Autowired
    private ApplyRecordRepository applyRecordRepository;
    @Autowired
    private RoleRepository roleRepository;

    @Override
    public AppPage<ClassRoomVO> findPage(String keyword, RoomType type, PageRequest pageRequest) {
        Page<ClassRoom> page = classRoomRepository.findAll(keyword, type, pageRequest);
        List<ClassRoomVO> list = page.getContent().stream().map(ClassRoomVO::new).collect(Collectors.toList());
        return new AppPage<>(pageRequest, page.getTotalElements(), page.getTotalPages(), list);
    }

    @Override
    public void save(ClassRoom classRoom, Admin currentInfo) {
        Role teacherRole = roleRepository.findByNameLike("%教师%");
        if (currentInfo.getRoles().contains(teacherRole)) {
            throw new BusinessException(ApiResult.NO_PERMISSIONS);
        }
        Date now = new Date();
        classRoom.setModifyDate(now);
        classRoom.setCreateDate(now);
        classRoom.setStatus(false);
        classRoomRepository.save(classRoom);
    }

    @Override
    public ClassRoomVO findDetail(Long id) {
        return new ClassRoomVO(classRoomRepository.findOne(id));
    }

    @Override
    public void apply(Long id, Admin currentInfo) {
        Date now = new Date();
        ClassRoom room = classRoomRepository.findOne(id);
        if (Objects.isNull(room)) {
            throw new BusinessException(ApiResult.DATA_NOT_FOUND, "教室信息不存在");
        }
        if (room.getStatus()) {
            throw new BusinessException(ApiResult.APPLY_FAIL);
        }
        room.setStatus(true);
        room = classRoomRepository.save(room);
        ApplyRecord applyRecord = new ApplyRecord();
        applyRecord.setCreateDate(now);
        applyRecord.setModifyDate(now);
        applyRecord.setStatus(ApplyStatus.APPLYING);
        applyRecord.setAdmin(currentInfo);
        applyRecord.setApplyTime(DateUtil.format(now, DatePattern.NORM_DATETIME_MINUTE_PATTERN));
        applyRecord.setRoom(room);
        applyRecord.setOverDue(false);
        applyRecordRepository.save(applyRecord);
    }

    @Override
    public void deleteRoom(Long id) {
        ClassRoom room = classRoomRepository.findOne(id);
        if (Objects.isNull(room)) {
            throw new BusinessException(ApiResult.DATA_NOT_FOUND, "教室信息不存在，不能进行回收");
        }
        if (room.getStatus()) {
            throw new BusinessException(ApiResult.RECYCLE_FAIL);
        }
        classRoomRepository.delete(room);
    }

    @Override
    public void returnRoom(Long id) {
        ClassRoom room = classRoomRepository.findOne(id);
        room.setModifyDate(new Date());
        room.setStatus(false);
        classRoomRepository.save(room);
    }
}

